<?php

namespace App\Jobs\OpenShop;

use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Service\OpenShop\OrderService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class OrderAutoCloseDelayJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public string $orderNo       = '';
    public string $closeDateTime = '';

    /**
     * 自动关闭签约订单
     *
     * @return void
     */
    public function __construct(string $orderNo, string $closeDateTime)
    {
        $this->orderNo = $orderNo;
        $seconds       = max(0, strtotime($closeDateTime) - time());
        $this->delay($seconds);
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $order = OpenShopSignOrderModel::query()
            ->where('orderNo', $this->orderNo)
            ->first();
        if ($order) {
            OrderService::close($order, 1);
        }
    }
}
